package com.kanqiubao.kq.activity.video;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.dueeeke.videocontroller.StandardVideoController;
import com.dueeeke.videocontroller.component.CompleteView;
import com.dueeeke.videocontroller.component.ErrorView;
import com.dueeeke.videocontroller.component.GestureView;
import com.dueeeke.videocontroller.component.PrepareView;
import com.dueeeke.videocontroller.component.TitleView;
import com.dueeeke.videocontroller.component.VodControlView;
import com.dueeeke.videoplayer.player.VideoView;
import com.kanqiubao.kq.R;
import com.kanqiubao.kq.ThisAppApplication;
import com.kanqiubao.kq.activity.BaseActivity;
import com.kanqiubao.kq.fragment.video.VideoListFragment;

import androidx.fragment.app.FragmentTransaction;

/**
 * 视频播放详细
 */
public class VideoDetailActiviy extends BaseActivity<String> {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setToolbar();
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        mVideoView = findView(R.id.video_id);
        initVideoView();
        intFragment();
    }

    @Override
    protected int getActivitylayout() {
        return R.layout.video_detail_layout;
    }

    @Override
    protected String getTitleText() {
        return null;
    }

    private VideoListFragment mVideoListFragment;

    private void intFragment() {
        mVideoListFragment = new VideoListFragment();
        mVideoListFragment.setmType(1);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.fg_id, mVideoListFragment);
        ft.commit();
    }

    protected VideoView mVideoView;
    protected StandardVideoController mController;
    protected ErrorView mErrorView;
    protected CompleteView mCompleteView;
    protected TitleView mTitleView;


    protected void initVideoView() {
        mController = new StandardVideoController(this);
        PrepareView prepareView = new PrepareView(this);//准备播放界面
        ImageView thumb = prepareView.findViewById(R.id.thumb);//封面图
        ThisAppApplication.loadImage("https://cms-bucket.nosdn.127.net/eb411c2810f04ffa8aaafc42052b233820180418095416.jpeg", thumb);
        mController.addControlComponent(prepareView);
        mErrorView = new ErrorView(this);
        mController.addControlComponent(mErrorView);
        mCompleteView = new CompleteView(this);
        mController.addControlComponent(mCompleteView);
        mTitleView = new TitleView(this);
        mController.addControlComponent(mTitleView);
        mController.addControlComponent(new VodControlView(this));
        mController.addControlComponent(new GestureView(this));
        mController.setEnableOrientation(true);
        mVideoView.setVideoController(mController);
        mVideoView.setScreenScaleType(VideoView.SCREEN_SCALE_CENTER_CROP);
        mVideoView.setUrl("http://vfx.mtime.cn/Video/2019/02/04/mp4/190204084208765161.mp4");
        mVideoView.start();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }

    private void setToolbar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mVideoView.resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mVideoView.pause();
    }

    @Override
    protected void onDestroy() {
        mVideoView.release();
        super.onDestroy();
    }
}
